/* TODO: Change toolbox XML ID if necessary. Can export toolbox XML from Workspace Factory. */
var toolbox = document.getElementById("toolbox");

var options = { 
	toolbox : toolbox, 
	collapse : false, 
	comments : false, 
	disable : false, 
	maxBlocks : Infinity, 
	trashcan : false, 
	horizontalLayout : false, 
	toolboxPosition : 'start', 
	css : true, 
	media : 'https://blockly-demo.appspot.com/static/media/', 
	rtl : false, 
	scrollbars : false, 
	sounds : true, 
	oneBasedIndex : true
};

/* Inject your workspace */ 
var workspace = Blockly.inject('blockDiv', options);

/* Add the background image */
var svg = workspace.getParentSvg();
var work = svg.getElementsByClassName("blocklyWorkspace");
var xmlns = "http://www.w3.org/2000/svg";
var bg = document.createElementNS(xmlns, 'clipPath');
bg.setAttribute('id', 'bgPath');
var clipRect = document.createElementNS(xmlns, 'rect');
clipRect.setAttribute('id', 'clipRect');
clipRect.setAttribute('width', 400);
clipRect.setAttribute('height', 225);
clipRect.setAttribute('x', 300);
clipRect.setAttribute('y', 100);
clipRect.setAttribute('border', '1');
bg.appendChild(clipRect);
svg.appendChild(bg);

var bgImage = document.createElementNS(xmlns, 'image');
bgImage.setAttribute('id','background');
bgImage.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href',
    '../res1/pig.jpg');
bgImage.setAttribute('width', 400);
bgImage.setAttribute('height', 225);
bgImage.setAttribute('x', 300);
bgImage.setAttribute('y', 100);
bgImage.setAttribute('border', '1px solid red');
bgImage.setAttribute('opacity', 0.4);
bgImage.setAttribute('clip-path', 'url(#bgPath)');
svg.appendChild(bgImage);
svg.appendChild(work[0]);

/* Load Workspace Blocks from XML to workspace. Remove all code below if no blocks to load */

/* TODO: Change workspace blocks XML ID if necessary. Can export workspace blocks XML from Workspace Factory. */
var workspaceBlocks = document.getElementById("workspaceBlocks"); 

/* Load blocks to workspace. */
Blockly.Xml.domToWorkspace(workspaceBlocks, workspace);

function button_click() {
	var code = Blockly.JavaScript.workspaceToCode(workspace);
	console.log(code);
	if (code == "#pig1#pig2#pig3") {
		alert("Correct!");
		window.location.href="course1-1-6.html"
	} else {
		alert("Failed!");
	}
}

document.getElementById("check_button").addEventListener("click", button_click);